function registerSW() {
  if (!window.navigator.serviceWorker) {
    console.warn('Service Worker is not supported');
    return
  }
  // 注册Service Worker，scope 控制serviceWorker的生效范围
  window.navigator.serviceWorker.register('./sw.js', { scope: '/' })
  .then((registration) => {
     registration.addEventListener('updatefound', function () {
          const newWorker = registration.installing;
          newWorker.addEventListener('statechange', function () {
              if (newWorker.state === 'activated') {
                  // 可以在这里提示用户有新的 Service Worker 生效，可能需要刷新页面
                  console.log('新的 Service Worker 已激活');
              }
          });
      });
  })
  .catch((err) => {
    console.warn('service worker registration failed', err)
  })
}

// 等待页面加载完成，再注册Service Worker
window.addEventListener('load', registerSW)

document.getElementById('btn').addEventListener('click', () => {
  renderDog()
})

function renderDog() {
  const img = new Image()
  img.src = '/img/dog.svg';
  img.width = 200;
  img.style.display = 'block';
  document.body.appendChild(img);
}